<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>新增页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form" action="">

            <div class="mainBox">
                <div class="main-container mr-5">

                    <div class="layui-form-item">
                        <label class="layui-form-label">发放人群</label>
                        <div class="layui-input-block">
                            <input type="radio"  name="type" lay-filter="type" class="layui-radio" title="所有用户" value="2" checked>
                            <input type="radio"  name="type" lay-filter="type" class="layui-radio" title="部分用户" value="1">
                        </div>
                    </div>
                    
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <input type="hidden" name="type" value="1">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" id="next-button" lay-submit=""
                        lay-filter="save">
                        发放
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        
        <script>

            let couponId = <?=$id?>;
            // 相关接口
            const INSERT_API = "/app/admin/coupon/give";
            
            // 字段 截止时间 deadline
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#deadline",
                    type: "datetime",
                });
            })
            
            //提交事件
            layui.use(["form", "popup", "common"], function () {
                let $ = layui.$;
                let common = layui.common;
                layui.form.on('radio(type)', function(data){

                    if (data.value == 1) {
                        $('#next-button').html('下一步')
                    } else {
                        $('#next-button').html('发放')
                    }
                });

                // 字段验证允许为空
                layui.form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });
                layui.form.on("submit(save)", function (data) {
                    let type = $("input[name=type]:checked").val();


                    if (type == 2) {
                        layui.$.ajax({
                            url: INSERT_API,
                            type: "POST",
                            dateType: "json",
                            data: {coupon_id: couponId},
                            success: function (res) {
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", function () {
                                    parent.refreshTable();
                                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                                });
                            }
                        });
                    } else {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                        parent.layer.open({
                            type: 2,
                            title: "修改",
                            shade: 0.1,
                            shadeClose:true,
                            area: ["800px", "650px"],
                            content: '/app/admin/user/search?type=coupon&id=' + couponId
                        });
                    }


                    return false;
                });
            });

            layui.use(["jquery", "xmSelect"], function() {
                layui.$.ajax({
                    url: "/app/admin/goods-category/tree",
                    dataType: "json",
                    success: function (res) {
                        let value = layui.$("#goods_category_ids").attr("value");
                        let initValue = value ? value.split(",") : [];
                        layui.xmSelect.render({
                            tree: {
                                show: true,
                                strict: false,
                            },
                            el: "#goods_category_ids",
                            name: "goods_category_ids",
                            initValue: initValue,
                            data: res.data,
                            model: {"icon":"hidden","label":{"type":"text"}},
                        });
                        if (res.code) {
                            return layui.popup.failure(res.msg);
                        }
                    }
                });
            });

        </script>

    </body>
</html>
